package com.qingfeng.testandroid12sdkdemo.network

import android.content.Context
import android.util.AttributeSet
import android.view.ViewGroup
import androidx.annotation.AttrRes
import com.qingfeng.basic_lib.basic.BasicModel
import com.qingfeng.testandroid12sdkdemo.data.db.dao.HomeDao
import com.qingfeng.testandroid12sdkdemo.data.entity.*
import com.qingfeng.testandroid12sdkdemo.network.http.HomeNetWork

class HomeRepository private constructor(
    private val netWork: HomeNetWork,
    private val localData: HomeDao
): BasicModel(){

    suspend fun getBannerData(refresh: Boolean = false): List<BannerBean> {
        return cacheNetCall({
                netWork.getBannerData()
            },{
                localData.getBannerList()
            },{
            if (refresh) localData.deleteBannerAll()
            localData.insertBanner(it)
            },{
            !refresh && !it.isNullOrEmpty()
            }
        )
    }


    suspend fun getHomeList(page: Int, refresh: Boolean): HomeListBean {
        return cacheNetCall({
            netWork.getHomeList(page)
        },{
            localData.getHomeList(page + 1)
        },{
          if (refresh) localData.deleteBannerAll()
          localData.insertData(it)
        },{
            !refresh
        })
    }


    suspend fun getNaviJson(): BaseResult<List<NavTypeBean>> {
        return netWork.getNaviJson()
    }

    suspend fun getProjectList(page: Int, cid: Int): BaseResult<HomeListBean> {
        return netWork.getProjectList(page, cid)
    }

    suspend fun getPopularWeb(): BaseResult<MutableList<UsedWeb>> {
        return netWork.getPopularWeb()
    }

    companion object {

        @Volatile
        private var INSTANCE : HomeRepository? = null

        fun getInstance(netWork: HomeNetWork, homeDao: HomeDao) =
            INSTANCE ?: synchronized(this) {
                INSTANCE?: HomeRepository(netWork,homeDao).also { INSTANCE = it }
            }
    }

}

